package com.blog.wu.myblog.activemq.subscribe;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Author wuyanshen
 * @Date 2018-12-25 1:19 PM
 * @Discription 发布订阅通讯 一对多 可以有多个消费者 activeMQ原生使用方式
 */
public class ActiveMqProducer {

    public static void main(String[] args) {

        try {
            //获取mq连接工厂
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://127.0.0.1:32768");
            //创建连接
            Connection connection = connectionFactory.createConnection();
            //启动连接
            connection.start();
            //创建会话工厂
            Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            //创建队列
//            Destination destination = session.createQueue("myblog_queue");
            MessageProducer producer = session.createProducer(null);
            //不持久化
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            for (int i =0;i<5;i++) {
                System.out.println("我是生产者"+i);
                sendMsg(session,producer,"我是生产者"+i);
            }

            System.out.println("生产者发送完毕");

        } catch (JMSException e) {
            e.printStackTrace();
        }
    }


    public static void sendMsg(Session session,MessageProducer producer,String i) throws JMSException {
        TextMessage textMessage = session.createTextMessage("hello activemq"+i);
        Destination destination = session.createTopic("myblog_topic");
        producer.send(destination,textMessage);
    }
}
